﻿using System;
using System.Timers;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using MyCalManager.Data;
using MyCalManager.Helper;
using MyCalManager.Properties;

namespace MyCalManager.ViewModel
{
    
    public class MainViewModel : ViewModelBase
    {

        private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
        private CoreData data = CoreData.Instance;

        private Uri Info = new Uri("../Images/Info.png", UriKind.RelativeOrAbsolute);
        private Uri Question = new Uri("../Images/Question.png", UriKind.RelativeOrAbsolute);
        private Uri Error = new Uri("../Images/Error.png", UriKind.RelativeOrAbsolute);
        private Uri Alert = new Uri("../Images/Alert.png", UriKind.RelativeOrAbsolute);
        

        private Timer timer = new Timer(10000);
        private bool IsTimerOn = false;
              

        public MainViewModel()
        {
            Initialize();
           
        }

        private void Initialize()
        {
            this.timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
            this.data.UserID = Settings.Default.UserID;

            Messenger.Default.Register<CustomDialogMessage>(
               this,
               m =>
               {
                   SetMsgBox(m);
               });
            Messenger.Default.Register<Navigator>(
               this,
               m =>
               {
                   navigateTO(m);
               });
            Messenger.Default.Register<bool>(
               this,
               m =>
               {
                   isLoaded(m);
               });

            this.UserPage = new RelayCommand(
                 () => this.Page = new Uri("View/UserView.xaml", UriKind.RelativeOrAbsolute));

            this.Close = new RelayCommand(
                 () => OnClose());

            //MsgBox Cmds
            this.Yes = new RelayCommand(
                () => OnYes());

            this.No = new RelayCommand(
                () => OnNo());

            this.Ok = new RelayCommand(
                () => OnOk());

            this.Cancel = new RelayCommand(
                () => OnCancel());

            this.EditFoodDB = new RelayCommand(
                () => Messenger.Default.Send(EditType.FoodDB));

            this.EditUserDB = new RelayCommand(
                () => Messenger.Default.Send(EditType.UserDB) );

            this.MonthGraph = new RelayCommand(
                () => this.Page = new Uri("View/MonthView.xaml", UriKind.RelativeOrAbsolute));

            this.YearGraph = new RelayCommand(
                () => this.Page = new Uri("View/YearView.xaml", UriKind.RelativeOrAbsolute));

            this.About = new RelayCommand(
               () => this.Page = new Uri("View/AboutView.xaml", UriKind.RelativeOrAbsolute));

        }

        

        private void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            CustomDialogMessage m = new CustomDialogMessage();
            m.Content = string.Empty;
            m.customMessageBoxImage = CustomMessageBoxImage.None;
            this.SetMsgBox(m);
            this.timer.Stop();
            this.IsTimerOn = false;
        }

        private void navigateTO(Navigator m)
        {
            if (m.Result == NavigateResult.OK)
            {

            }
            this.Page = m.To.EnumToURI();
        }

        private void isLoaded(bool m)
        {
            if (m)
            {
                if (this.data.LastError == "NODBFILE")
                    Messenger.Default.Send("NODBFILE");
            }
        }



        #region Public Propertys

        public const string PagePropertyName = "Page";
        private Uri _Page = new Uri("View/MainContent.xaml", UriKind.RelativeOrAbsolute);
        public Uri Page
        {
            get
            {
                return _Page;
            }

            set
            {
                if (_Page == value)
                {
                    return;
                }


                _Page = value;

                // Update bindings, no broadcast
                RaisePropertyChanged(PagePropertyName);

            }
        }



        public const string BarImagePropertyName = "BarImage";
        private Uri _BarImage;
        public Uri BarImage
        {
            get
            {
                return _BarImage;
            }

            set
            {
                if (_BarImage == value)
                {
                    return;
                }


                _BarImage = value;

                // Update bindings, no broadcast
                RaisePropertyChanged(BarImagePropertyName);

            }
        }

        public const string MessagePropertyName = "Message";
        private string _Message;
        public string Message
        {
            get
            {
                return _Message;
            }

            set
            {
                if (_Message == value)
                {
                    return;
                }


                _Message = value;

                // Update bindings, no broadcast
                RaisePropertyChanged(MessagePropertyName);

            }
        }

        public const string ShowWaitPropertyName = "ShowWait";
        private bool _ShowWait = false;
        public bool ShowWait
        {
            get
            {
                return _ShowWait;
            }

            set
            {
                if (_ShowWait == value)
                {
                    return;
                }

                _ShowWait = value;

                // Update bindings, no broadcast
                RaisePropertyChanged(ShowWaitPropertyName);

            }
        }

        
        

        #endregion

        #region Commands and CommandMethod

        public RelayCommand UserPage
        {
            get;
            private set;
        }

        public RelayCommand Close
        {
            get;
            private set;
        }

        public RelayCommand EditFoodDB
        {
            get;
            private set;
        }

        public RelayCommand EditUserDB
        {
            get;
            private set;
        }

        public RelayCommand MonthGraph
        {
            get;
            private set;
        }

        public RelayCommand YearGraph
        {
            get;
            private set;
        }

        public RelayCommand About
        {
            get;
            private set;
        }
        

        private void OnClose()
        {
            Messenger.Default.Send("Close");
        }

        private void OnAddUser()
        {
            throw new NotImplementedException();
        }

        #endregion

        #region MsgBox Adorner

        private CustomDialogMessage LastDialogMessage { get; set; }

        private void SetMsgBox(CustomDialogMessage m)
        {
            if (m.isDialog)
            {
                this.customMessageBoxImage = m.customMessageBoxImage;
                this.customMessageBoxButton = m.customMessageBoxButton;
                this.InfoText = m.Content;
                LastDialogMessage = m;
                this.ShowWait = true;
            }
            else
            {
                switch (m.customMessageBoxImage)
                {
                    case CustomMessageBoxImage.Error:
                        this.BarImage = Error;
                        break;
                    case CustomMessageBoxImage.Information:
                        this.BarImage = Info;
                        break;
                    case CustomMessageBoxImage.Question:
                        this.BarImage = Question;
                        break;
                    case CustomMessageBoxImage.Alert:
                        this.BarImage = Alert;
                        break;
                    case CustomMessageBoxImage.None:
                        this.BarImage = null;
                        break;
                    default:
                        break;
                }
                this.Message = m.Content;
                if (this.IsTimerOn)
                {
                    this.timer.Stop();
                    this.timer.Start();
                }
                else
                {
                    this.timer.Start();
                    this.IsTimerOn = true;
                }
            }
        }

        public const string InfoTextPropertyName = "InfoText";
        private string _InfoText;
        public string InfoText
        {
            get
            {
                return _InfoText;
            }

            set
            {
                if (_InfoText == value)
                {
                    return;
                }


                _InfoText = value;


                // Update bindings, no broadcast
                RaisePropertyChanged(InfoTextPropertyName);


            }
        }

        
        public const string visibilityYesPropertyName = "visibilityYes";
        private bool _visibilityYes = false;
        public bool visibilityYes
        {
            get
            {
                return _visibilityYes;
            }

            set
            {
                if (_visibilityYes == value)
                {
                    return;
                }


                _visibilityYes = value;

                // Update bindings, no broadcast
                RaisePropertyChanged(visibilityYesPropertyName);


            }
        }

        
        public const string visibilityNoPropertyName = "visibilityNo";
        private bool _visibilityNo = false;
        public bool visibilityNo
        {
            get
            {
                return _visibilityNo;
            }

            set
            {
                if (_visibilityNo == value)
                {
                    return;
                }


                _visibilityNo = value;

                // Update bindings, no broadcast
                RaisePropertyChanged(visibilityNoPropertyName);


            }
        }

        
        public const string visibilityOkPropertyName = "visibilityOk";
        private bool _visibilityOk = false;
        public bool visibilityOk
        {
            get
            {
                return _visibilityOk;
            }

            set
            {
                if (_visibilityOk == value)
                {
                    return;
                }


                _visibilityOk = value;

                // Update bindings, no broadcast
                RaisePropertyChanged(visibilityOkPropertyName);


            }
        }

        
        public const string visibilityCancelPropertyName = "visibilityCancel";
        private bool _visibilityCancel = false;
        public bool visibilityCancel
        {
            get
            {
                return _visibilityCancel;
            }

            set
            {
                if (_visibilityCancel == value)
                {
                    return;
                }


                _visibilityCancel = value;

                // Update bindings, no broadcast
                RaisePropertyChanged(visibilityCancelPropertyName);
            }
        }

        
        public const string ImagePropertyName = "Image";
        private Uri _Image;
        public Uri Image
        {
            get
            {
                return _Image;
            }

            set
            {
                if (_Image == value)
                {
                    return;
                }


                _Image = value;


                // Update bindings, no broadcast
                RaisePropertyChanged(ImagePropertyName);

            }
        }


        public CustomMessageBoxImage customMessageBoxImage
        {
            set
            {
                switch (value)
                {
                    case CustomMessageBoxImage.Error:
                        this.Image = Error;
                        break;
                    case CustomMessageBoxImage.Information:
                        this.Image = Info;
                        break;
                    case CustomMessageBoxImage.Question:
                        this.Image = Question;
                        break;
                    case CustomMessageBoxImage.Alert:
                        this.Image = Alert;
                        break;
                    case CustomMessageBoxImage.None:
                        break;
                    default:
                        break;
                }
            }
        }

        public CustomMessageBoxButton customMessageBoxButton
        {
            set
            {
                this.visibilityOk = false;
                this.visibilityCancel = false;
                this.visibilityYes = false;
                this.visibilityNo = false;
                switch (value)
                {
                    case CustomMessageBoxButton.OK:
                        this.visibilityOk = true;
                        break;
                    case CustomMessageBoxButton.OKCancel:
                        this.visibilityOk = true;
                        this.visibilityCancel = true;
                        break;
                    case CustomMessageBoxButton.YesNo:
                        this.visibilityYes = true;
                        this.visibilityNo = true;
                        break;
                    case CustomMessageBoxButton.YesNoCancel:
                        this.visibilityYes = true;
                        this.visibilityNo = true;
                        this.visibilityCancel = true;
                        break;
                    default:
                        break;
                }
            }
        }

        public CustomDialogMessageResult result { get; set; }


        public RelayCommand Yes
        {
            get;
            private set;
        }

        public RelayCommand No
        {
            get;
            private set;
        }

        public RelayCommand Ok
        {
            get;
            private set;
        }

        public RelayCommand Cancel
        {
            get;
            private set;
        }


        private void OnYes()
        {
            this.result = CustomDialogMessageResult.Yes;
            this.ShowWait = false;
            LastDialogMessage.ProcessCallback(CustomDialogMessageResult.Yes);
        }

        private void OnNo()
        {
            this.result = CustomDialogMessageResult.No;
            this.ShowWait = false;
            LastDialogMessage.ProcessCallback(CustomDialogMessageResult.No);
        }

        private void OnOk()
        {
            this.result = CustomDialogMessageResult.OK;
            this.ShowWait = false;
            LastDialogMessage.ProcessCallback(CustomDialogMessageResult.OK);
        }

        private void OnCancel()
        {
            this.result = CustomDialogMessageResult.Cancel;
            this.ShowWait = false;
            LastDialogMessage.ProcessCallback(CustomDialogMessageResult.Cancel);
        }



        #endregion

        
    }

        



}